﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace cadbom
{
    public partial class InputProduct : Form
    {
        Action<BasProduct> callback;
        BasProduct product;
        public InputProduct(Action<BasProduct> callback)
        {
            this.callback = callback;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text != "")
            {
                this.checkProduct();
                this.Close();
            }
            else
            {
                MessageBox.Show("必须输入复制产品代码");
            }

        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkProduct()
        {
            String productCode = this.textBox1.Text;
            //获取了DOMAIN"1001-000011-00"
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basProduct/list?queryName=" + this.textBox1.Text, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<BasProduct> cc = JsonConvert.DeserializeObject<ResponResultA<BasProduct>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<BasProduct> products = cc.result.records;
                        product = new BasProduct();
                        if (products != null && products.Count() == 1)
                        {
                            product = products[0];
                            if (product.code != null && productCode.Trim() == product.code.Trim())
                            {
                                if (callback != null)
                                {
                                    callback.Invoke(product);
                                }
                            }
                            else
                            {
                                MessageBox.Show("没找到复制产品数据！");
                            }
                        }
                        else
                        {
                            MessageBox.Show("没找到复制产品数据！");
                        }
                    }
                    else
                    {
                        MessageBox.Show("接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    MessageBox.Show("网络连接错误: " + r11.Error);
                }
            }));
        }


    }
}
